package com.sundaying.gooway.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sundaying.gooway.entity.User;
import com.sundaying.gooway.service.UserService;

/**
 * @author Administrator
 * 
 */
public class UserAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	// 将会被Struts2序列化为JSON字符串的对象
	private Map<String, Object> dataMap = new HashMap<String, Object>();

	private int id;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private String password;

	private String userName;

	private String certCode;
	
	private List<User> userList;
	
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String getCertCode() {
		return certCode;
	}

	public void setCertCode(String certCode) {
		this.certCode = certCode;
	}

	private UserService userService;

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public String getPassword() {
		return password;
	}

	public String getUserName() {
		return userName;
	}

	public UserService getUserService() {
		return userService;
	}

	@Override
	public void validate() {
		System.out.println("validate begin...");
		// 所有action都会调此处。例如：用户登录
//		if ("".equals(this.getUsername()) || this.getUsername().length() < 6) {
//			this.addFieldError("username", "请输入长度大于5个字符的用户名!");
//		}
		System.out.println("validate end...");
	}

	// 注册用户
	public String reg() {
		System.out.println(this.getUserName());
		if ("".equals(this.getUserName())) {
			return "error";
		} else {
			// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
			dataMap.clear();

			User user = new User();
			user.setUserName(userName);
			user.setPassword(password);
			user.setBirthdate(new Date());

			dataMap.put("user", user);
			dataMap.put("success", true);

			int result = userService.add(user);
			if (result == 0)
				return "error";
		}

		return "success";
	}

	// 用户登录系统
	public String login() {
		dataMap.clear();
		
/*		// 校对检验码是否正确
		Map session = ActionContext.getContext().getSession();
		String certCode = session.get("certCode").toString();
		if (!certCode.equals(this.certCode)) {
			dataMap.put("success", false);
			dataMap.put("msg", "验证码错误");
			return this.SUCCESS;
		}
		*/
		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);
		
		int result = userService.login(user);
		if (result == 1) {
			dataMap.put("success", true);
		} else {
			dataMap.put("success", false);
			dataMap.put("msg", "用户不存在或密码错误");
		}
		return this.SUCCESS;
	}
	
	public String list(){
		userList = userService.list();
		return "success";
	}
	
	public String update(){
		System.out.println(user.getId());
		userService.update(user);
		return this.SUCCESS;
	}
	
	public String delete(){
		userService.delete(id);
		return this.SUCCESS;
	}
	
	public String add(){
		userService.add(user);
		return this.SUCCESS;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Resource(name = "userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
